package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.IArticleDao;
import com.magicode.cms.entiy.base.Article;
import com.magicode.cms.entiy.base.User;
import com.magicode.cms.service.IContributeService;
import com.magicode.common.dao.ICommonDaoService;

@Service("servicecontribute")
public class ContributeServiceImpl extends BaseServiceImpl<Article, IArticleDao> implements IContributeService {

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "author");
		map.put(json_title, "账号/邮箱/手机");
		map.put("width", 80);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "isUnique");
		map.put(json_title, "版权");
		map.put("width", 50);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "uniqueTime");
		map.put(json_title, "几日转为独家");
		map.put("width", 70);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "投稿标题");
		map.put("width", 170);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "userCommentsTextString");
		map.put(json_title, "给编辑的留言");
		map.put("width", 120);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "createTime");
		map.put(json_title, "投稿时间");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "contributeStatus");
		map.put(json_title, "审核状态");
		map.put("width", 50);
		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql ="from " +Article.class.getName()+" "+entity_alias_name+" ";

		hql += " left join "+entity_alias_name+".readOperate as ro ";
		hql += " left join "+entity_alias_name+".channel as ch ";
		hql += " left join "+entity_alias_name+".author as au ";
		hql += " left join "+entity_alias_name+".uniqueTime as ut ";
		hql += " left join "+entity_alias_name+".contributeStatus as cs ";
		hql += " left join "+entity_alias_name+".reportType as rt ";
		if(null!=map && !map.isEmpty()){
			hql += " where 1 = 1 and "+entity_alias_name+".isContribute = 1  ";
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+map.get(field_id)+"'";
			}
			if(map.containsKey("title")){
				hql = hql+" and "+entity_alias_name+".title like '%"+map.get("title")+"%'";
			}
			if(map.containsKey("status")){
				hql = hql+" and "+entity_alias_name+".status = '"+map.get("status")+"'";
			}
			if(map.containsKey("channelId")){
				hql = hql+" and "+entity_alias_name+".channel.id = '"+map.get("channelId")+"'";
			}
			if(map.containsKey("operatelId")){
				hql = hql+" and "+entity_alias_name+".readOperate.id = '"+map.get("operatelId")+"'";
			}
			if(map.containsKey("authorId")){
				hql = hql+" and "+entity_alias_name+".author.id = '"+map.get("authorId")+"'";
			}
			if(map.containsKey("statusId")){
				hql = hql+" and "+entity_alias_name+".contributeStatus.id = '"+map.get("statusId")+"'";
			}
		}
		return hql;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		List<Map<String, Object>> resultList = null;
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			hql += " and cs.title != '草稿' order by t.createTime desc ";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("createTime", sql_date_method+"("+entity_alias_name+".createTime  ,'"+sql_time_format+"' ) as createTime ");
			gridsel.put("isUnique", "case when "+entity_alias_name+".isUnique = 1 then '非独家' else '独家' end as isUnique ");
			gridsel.put("userCommentsTextString", ""+entity_alias_name+".userCommentsText as userCommentsTextString ");
//			gridsel.put("authorStr", ""+entity_alias_name+".authorStr as authorStr ");
			gridsel.put("contributeStatus", "cs.title as contributeStatus ");
			gridsel.put("uniqueTime", "ut.title as uniqueTime ");
			gridsel.put("author", "au.loginName as author ");
			resultList = this.queryMapDataList(hql, gridsel, start, size);
			resultList = super.convertByteToString(resultList,"userCommentsTextString");
			return resultList;
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}

	@Override
	public List<JsonOperation> getOpts(String url,String id) {
		List<JsonOperation> opts = new ArrayList<JsonOperation> ();
		JsonOperation opt= new JsonOperation();
		opt.setName("修改");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/getinfo/"+id+"/edit"+url);
		opt.setType("edit");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("删除");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/"+id+"?_requestMethod=delete");
		opt.setType("delete2");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("预览");
		opt.setUrl(getCtx().getContextPath()+"/p/article/sec/"+id+"");
		opt.setType("preview");
		opts.add(opt);
		return opts;
	}
	
	@Override
	public List<Article> getContributeByOwn(User user) throws Exception {
		List<Article> articleList = new ArrayList();
		Map<String, String> map = new HashMap<String, String>();
		map.put("authorId", user.getId());
//		String hql = this.getHql(map);
		String hql ="from " +Article.class.getName()+" "+entity_alias_name+" ";
		hql += " left outer join fetch "+entity_alias_name+".contributeStatus as cs ";
		if(null!=map && !map.isEmpty()){
			hql += " where 1 = 1 and "+entity_alias_name+".isContribute = 1 ";
			if(map.containsKey("authorId")){
				hql = hql+" and "+entity_alias_name+".author.id = '"+map.get("authorId")+"'";
			}
		}
		hql += " order by t.createTime desc ";
		articleList = commonDaoService.queryList(hql, null);
		return articleList;
	}
	
	@Override
	public IArticleDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return articleDao;
	}

	@Autowired
	@Qualifier("daoarticle")
	private IArticleDao articleDao;
	
	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;

	public IArticleDao getArticleDao() {
		return articleDao;
	}

	public void setArticleDao(IArticleDao articleDao) {
		this.articleDao = articleDao;
	}

	public ICommonDaoService getCommonDaoService() {
		return commonDaoService;
	}

	public void setCommonDaoService(ICommonDaoService commonDaoService) {
		this.commonDaoService = commonDaoService;
	}

}
